package com.snpic.appaw.module.insure.controller.app.vehicleregistration;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.snpic.appaw.framework.common.pojo.PageParam;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.pojo.CommonResult;
import com.snpic.appaw.framework.common.util.object.BeanUtils;
import static com.snpic.appaw.framework.common.pojo.CommonResult.success;

import com.snpic.appaw.framework.excel.core.util.ExcelUtils;

import com.snpic.appaw.framework.apilog.core.annotation.ApiAccessLog;
import static com.snpic.appaw.framework.apilog.core.enums.OperateTypeEnum.*;

import com.snpic.appaw.module.insure.controller.app.vehicleregistration.vo.*;
import com.snpic.appaw.module.insure.dal.dataobject.vehicleregistration.VehicleRegistrationDO;
import com.snpic.appaw.module.insure.service.vehicleregistration.VehicleRegistrationService;

@Tag(name = "C端 - 车险出单 - 车辆登记(暂时废弃)")
@RestController
@RequestMapping("/inusre/vehicle-registration")
@Validated
public class AppVehicleRegistrationController {

    @Resource
    private VehicleRegistrationService vehicleRegistrationService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆登记")
    public CommonResult<Long> createVehicleRegistration(@Valid @RequestBody AppVehicleRegistrationSaveReqVO createReqVO) {
        return success(vehicleRegistrationService.createVehicleRegistration(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆登记")
    public CommonResult<Boolean> updateVehicleRegistration(@Valid @RequestBody AppVehicleRegistrationSaveReqVO updateReqVO) {
        vehicleRegistrationService.updateVehicleRegistration(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆登记")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteVehicleRegistration(@RequestParam("id") Long id) {
        vehicleRegistrationService.deleteVehicleRegistration(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除车辆登记")
    public CommonResult<Boolean> deleteVehicleRegistrationList(@RequestParam("ids") List<Long> ids) {
        vehicleRegistrationService.deleteVehicleRegistrationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆登记")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppVehicleRegistrationRespVO> getVehicleRegistration(@RequestParam("id") Long id) {
        VehicleRegistrationDO vehicleRegistration = vehicleRegistrationService.getVehicleRegistration(id);
        return success(BeanUtils.toBean(vehicleRegistration, AppVehicleRegistrationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆登记分页")
    public CommonResult<PageResult<AppVehicleRegistrationRespVO>> getVehicleRegistrationPage(@Valid AppVehicleRegistrationPageReqVO pageReqVO) {
        PageResult<VehicleRegistrationDO> pageResult = vehicleRegistrationService.getVehicleRegistrationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppVehicleRegistrationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆登记 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVehicleRegistrationExcel(@Valid AppVehicleRegistrationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VehicleRegistrationDO> list = vehicleRegistrationService.getVehicleRegistrationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆登记.xls", "数据", AppVehicleRegistrationRespVO.class,
                        BeanUtils.toBean(list, AppVehicleRegistrationRespVO.class));
    }

}